//
//  SFWelcomeViewController.swift
//  SFWeiBo
//
//  Created by 孙凡 on 16/4/11.
//  Copyright © 2016年 Edward. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class SFWelcomeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()


    }





    func setupUI(){

        view.addSubview(iconView)

        view.addSubview(textLabel)


        //布置约束
        iconView.snp_makeConstraints { (make) in

            make.centerX.equalTo(self.view)
            make.centerY.equalTo(self.view).offset(-100)

            make.size.equalTo(CGSizeMake(90, 90))
        }

        textLabel.snp_makeConstraints { (make) in

            make.centerX.equalTo(self.view)
            make.centerY.equalTo(self.view).offset(-100)
        }



    }


    //MARK: - 视图加载完毕添加动画

    override func viewDidAppear(animated: Bool) {

        super.viewDidAppear(animated)

        //先更新约束
        iconView.snp_updateConstraints { (make) in

            make.centerY.equalTo(self.view).offset(-200)
        }


        /**
         参数1: 动画时间
         参数2: 延迟执行时间
         参数3: 阻尼,阻力(0->1)
         参数4: 弹簧的初始速度
         参数5: 枚举默认传0
         参数6: 动画
         参数7: 动画结束后做的事
         */
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: {

            self.view.layoutIfNeeded()



            }) { (_) in

                UIView.animateWithDuration(0.2, animations: {

                    self.textLabel.alpha = 1
                    }, completion: { (_) in

                        //跳转到主控制器
//                        self.presentViewController(SFMainTabBarController(), animated: true, completion: nil)
//                        let delegate = UIApplication.sharedApplication().delegate as! AppDelegate
//
//                        delegate.window?.rootViewController = SFMainTabBarController()
                        NSNotificationCenter.defaultCenter().postNotificationName(SFSwitchRootViewControllerNotification, object: self)

                })



        }

        
    }


    //MARK: - 懒加载控件
    lazy var iconView: UIImageView = {

        let imgView = UIImageView()


//        imgView.image = UIImage(named: "avatar_default_big")

        let imgString = SFUserAccountViewModel.sharedAccountViewModel.account?.avatar_large ?? ""

        let imgUrl = NSURL(string: imgString)

        imgView.sd_setImageWithURL(imgUrl, placeholderImage: UIImage(named: "avatar_default_big"))

        imgView.sizeToFit()


        imgView.layer.cornerRadius = 45
        imgView.layer.masksToBounds = true


        return imgView
    }()

    lazy var textLabel: UILabel = {

        let label = UILabel()

        label.text = "主人,欢迎回来"

        label.alpha = 0

        label.sizeToFit()

        return label

    }()

}
